/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { TaskDirectedAcyclicGraph } from '../../../../../src/base/internal/task/core/task-directed-acyclic-graph';

describe('Test task dag', () => {
  const dag = new TaskDirectedAcyclicGraph();

  test('test add remove node', () => {
    dag.addNode('task1');
    dag.addNode('task2');
    dag.addNode('task3');
    expect(dag.getAllStartNodes().size === 3);
    dag.removeNode('task2');
    expect(dag.getAllStartNodes().size === 2);
  });

  test('test add edge', () => {
    dag.addEdge('task1', 'task2');
    dag.addEdge('task2', 'task3');
    dag.addEdge('task1', 'task4');
    dag.addEdge('task3', 'task4');
    dag.addEdge('task4', 'task5');
    expect(dag.getAllStartNodes().size === 1);
    expect(dag.getAllEndNodes().size === 1);
    expect(dag.getChildren('task1')).toContain('task2');
    expect(dag.getChildren('task1')).toContain('task4');
    dag.removeNode('task3');
    expect(dag.getChildren('task2').size === 0);
    dag.clear();
    expect(dag.getAllEndNodes().size === 0);
  });

  /**
   * task1->task2->task3->task4->task5
   * task1->task4
   */
  test('test get children&parent node', () => {
    dag.addEdge('task1', 'task2');
    dag.addEdge('task2', 'task3');
    dag.addEdge('task1', 'task4');
    dag.addEdge('task3', 'task4');
    dag.addEdge('task4', 'task5');
    expect(dag.getParent('task4')).toEqual(new Set(['task1', 'task3']));

    expect(dag.getParent('task4')).toEqual(new Set(['task1', 'task3']));
    expect(dag.getParent('task1')).toEqual(new Set());
    expect(dag.getChildren('task1')).toEqual(new Set(['task2', 'task4']));
    expect(dag.getChildren('task4')).toEqual(new Set(['task5']));
    expect(dag.getChildren('task5')).toEqual(new Set());
  });
});
